package com.example.transactionhintassistant.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.transactionhintassistant.pojo.dto.user.AddUserInfoDto;
import com.example.transactionhintassistant.pojo.dto.user.ChangeUserInfoDto;
import com.example.transactionhintassistant.pojo.entity.User;
import com.example.transactionhintassistant.pojo.vo.RingtoneVo;
import com.example.transactionhintassistant.pojo.vo.UserInfoVo;
import com.example.transactionhintassistant.service.RingtoneSelectionService;
import com.example.transactionhintassistant.service.UsersService;
import com.example.transactionhintassistant.mapper.UsersMapper;
import com.example.transactionhintassistant.utils.JwtHelper;
import com.example.transactionhintassistant.utils.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86147
 * @description 针对表【users(用户表)】的数据库操作Service实现
 * @createDate 2024-03-04 22:44:59
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, User>
        implements UsersService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private RingtoneSelectionService ringtoneSelectionService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<UserInfoVo> getUserInfo() {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        User user = this.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user, userInfoVo);
        String email = user.getEmail();
        if (email != null) {
            userInfoVo.setEmail(email.substring(0, 2) + "***" + email.substring(email.indexOf("@")));
        }
        RingtoneVo ringtoneVo = ringtoneSelectionService.getSelectRingtoneByUserId(user.getId()).getData();
        userInfoVo.setRingtone(ringtoneVo);
        return Result.ok(userInfoVo);
    }

    @Override
    public Result<Object> changeUserInfo(ChangeUserInfoDto userInfoDto) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        User user = this.getById(userId);
        user.setNickname(userInfoDto.getNickname());
        user.setIntroduction(userInfoDto.getIntroduction());
        user.setAvatar(userInfoDto.getAvatar());
        this.updateById(user);
        return Result.ok(null, "成功修改！");
    }

    @Override
    public Result<Object> addUserInfo(AddUserInfoDto userInfoDto) {
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        User user = this.getById(userId);
        user.setNickname(userInfoDto.getNickname());
        user.setIntroduction(userInfoDto.getIntroduction());
        user.setAvatar(userInfoDto.getAvatar());
        user.setColorTheme(userInfoDto.getColorTheme());
        this.updateById(user);
        return Result.ok(null, "成功上传！");
    }

    @Override
    public Result<Object> changeUserColorTheme(String color) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, userId)
                .set(User::getColorTheme, color);
        this.update(updateWrapper);
        return Result.ok(null, "成功修改！");
    }

    @Override
    public Result<Object> deleteUser() {
        String token = request.getHeader("Authorization");
        int userId = jwtHelper.getUserId(token);
        this.removeById(userId);
        return Result.ok(null, "注销成功！");
    }
}




